package com.micro.db.jdbc;

import com.micro.db.dialect.IJdbcTemplate;
import com.micro.disk.user.bean.Page;
import com.micro.disk.user.bean.UserBean;
import com.micro.model.DiskUser;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class DiskUserJdbc {
	@Autowired
	private IJdbcTemplate jdbcTemplate;

	public Page<UserBean> findUserBeanList(Integer page, Integer limit,String username,String nickname){
		Page<UserBean> Page=new Page<>();
		try{
			String sql="select du.* from disk_user du where du.state = 1";

			if(!StringUtils.isEmpty(username)){
				sql+=" and du.username like '%"+username+"%' ";
			}

			if(!StringUtils.isEmpty(nickname)){
				sql+=" and du.nickname like '%"+nickname+"%' ";
			}

			Object[] args={};
			Page<Map<String,Object>> pi=jdbcTemplate.findPageList(sql, args, page, limit);

			List<Map<String,Object>> lists=pi.getRows();
			List<UserBean> rows=new ArrayList<UserBean>();
			if(!CollectionUtils.isEmpty(lists)){
				lists.forEach(m->{
					UserBean row=new UserBean();
					row.setId(m.get("id")==null?"":m.get("id").toString());
					row.setUsername(m.get("username")==null?"":m.get("username").toString());
					row.setNickname(m.get("nickname")==null?"":m.get("nickname").toString());
					row.setTelephone(m.get("telephone")==null?"":m.get("telephone").toString());
					rows.add(row);
				});
			}

			Page.setPage(page);
			Page.setLimit(limit);
			Page.setRows(rows);
			Page.setTotalElements(pi.getTotalElements());
			Page.setTotalPage(pi.getTotalPage());

			Page.setCode(0);
			Page.setMsg("查询成功");

		}catch(Exception e){
			e.printStackTrace();
			Page.setCode(1);
			Page.setMsg("查询失败");
		}
		return Page;

	}

	public Page<DiskUser> findDiskUserList(Integer page, Integer limit){
		Page<DiskUser> Page=new Page<>();
		try{
			String sql="select du.* from disk_user du where du.state = 1";
			Object[] args={};
			Page<Map<String,Object>> pi=jdbcTemplate.findPageList(sql, args, page, limit);

			List<Map<String,Object>> lists=pi.getRows();
			List<DiskUser> rows=new ArrayList<DiskUser>();
			if(!CollectionUtils.isEmpty(lists)){
				lists.forEach(m->{
					DiskUser row=new DiskUser();
					row.setId(m.get("id")==null?"":m.get("id").toString());
					row.setUsername(m.get("username")==null?"":m.get("username").toString());
					row.setNickname(m.get("nickname")==null?"":m.get("nickname").toString());
					row.setPassword(m.get("password")==null?"":m.get("password").toString());
					row.setTelephone(m.get("telephone")==null?"":m.get("telephone").toString());
					row.setOrgid(m.get("orgid")==null?"":m.get("orgid").toString());
					row.setState(m.get("state")==null?0:(int)m.get("state"));
					rows.add(row);
				});
			}

			Page.setPage(page);
			Page.setLimit(limit);
			Page.setRows(rows);
			Page.setTotalElements(pi.getTotalElements());
			Page.setTotalPage(pi.getTotalPage());

			Page.setCode(0);
			Page.setMsg("查询成功");

		}catch(Exception e){
			e.printStackTrace();
			Page.setCode(1);
			Page.setMsg("查询失败");
		}
		return Page;

	}
}
